package pzl.com.transitionsframework.transition;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author zl.peng
 * @version [1.0, 2016-12-05]
 *          <p/>
 *          只要一个动画统一刷新UI，其它动画只改变数据
 */
public class CustomTransition1 extends Transition {
    private static final String TAG = "CustomTransition1";

    /**
     * 是否是处于进入的动画（协调 进入场景和退出场景动画的正反播放）
     */

    private static final String KEY_CIV_TOP = "civ_top";
    private static final String KEY_CIV_LEFT = "civ_left";
    private static final String KEY_CIV_SIZE = "civ_size";

    private long durationCivSize = 2000;
    private long durationCivPosition = 2000;

    private TimeInterpolator mCivSizeInterpolator;
    private TimeInterpolator mCivPositionInterpolator;

    private static final String KEY_ITEM_TOP = "item_top";
    private static final String KEY_ITEM_HEIGHT = "item_height";

    private long durationItem = 2000;

    private TimeInterpolator mItemInterpolator;


    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        String transitionName = transitionValues.view.getTransitionName();
        View startView = transitionValues.view;
        if ("civ".equals(transitionName)) {
            transitionValues.values.put(KEY_CIV_LEFT, startView.getLeft());
            transitionValues.values.put(KEY_CIV_TOP, startView.getTop());
            transitionValues.values.put(KEY_CIV_SIZE, startView.getHeight());
        } else if ("container".equals(transitionName)) {
            transitionValues.values.put(KEY_ITEM_TOP, startView.getTop());
            transitionValues.values.put(KEY_ITEM_HEIGHT, startView.getHeight());
        }
    }

    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        String transitionName = transitionValues.view.getTransitionName();
        View endView = transitionValues.view;
        if ("civ".equals(transitionName)) {
            transitionValues.values.put(KEY_CIV_LEFT, endView.getLeft());
            transitionValues.values.put(KEY_CIV_TOP, endView.getTop());
            transitionValues.values.put(KEY_CIV_SIZE, endView.getHeight());
        } else if ("container".equals(transitionName)) {
            transitionValues.values.put(KEY_ITEM_TOP, endView.getTop());
            transitionValues.values.put(KEY_ITEM_HEIGHT, endView.getHeight());
        }
    }

    @Override
    public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
        if (startValues == null || endValues == null) {
            return null;
        }
        Animator anim = null;
        String transitionName = startValues.view.getTransitionName();
        if ("civ".equals(transitionName)) {
            anim = createCivAnim(sceneRoot, startValues, endValues);
        } else if ("container".equals(transitionName)) {
            anim = createItemAnim(sceneRoot, startValues, endValues);
        }
        return anim;
    }


    public void setDurationCivSize(long durationCivSize) {
        this.durationCivSize = durationCivSize;
    }

    public void setDurationCivPosition(long durationCivPosition) {
        this.durationCivPosition = durationCivPosition;
    }

    public void setmCivSizeInterpolator(TimeInterpolator mCivSizeInterpolator) {
        this.mCivSizeInterpolator = mCivSizeInterpolator;
    }

    public void setmCivPositionInterpolator(TimeInterpolator mCivPositionInterpolator) {
        this.mCivPositionInterpolator = mCivPositionInterpolator;
    }

    /**
     * 创建圆形imageview的动画
     *
     * @param sceneRoot
     * @param startValues
     * @param endValues
     * @return
     */
    private Animator createCivAnim(ViewGroup sceneRoot, TransitionValues startValues, final TransitionValues endValues) {
        final View endView = endValues.view;

        final int startTop = (int) startValues.values.get(KEY_CIV_TOP);
        final int startLeft = (int) startValues.values.get(KEY_CIV_LEFT);
        final int startSize = (int) startValues.values.get(KEY_CIV_SIZE);


        final int endTop = (int) endValues.values.get(KEY_CIV_TOP);
        final int endLeft = (int) endValues.values.get(KEY_CIV_LEFT);
        final int endSize = (int) endValues.values.get(KEY_CIV_SIZE);

        /**
         * 初始化 civ endView 的初始化状态-----------------------------
         */
        //position
        endView.setTop(startTop);
        endView.setBottom(startTop + startSize);
        endView.setLeft(startLeft);
        endView.setRight(startLeft + startSize);
        //size
        int curTop = endView.getTop();
        endView.setBottom(curTop + startSize);
        int curLeft = endView.getLeft();
        endView.setRight(curLeft + startSize);
        /**
         * 初始化 civ endView 的初始化状态-----------------------------
         */

        ValueAnimator topAnim = ValueAnimator.ofInt(startTop, endTop);
        topAnim.setDuration(durationCivPosition);
        topAnim.setInterpolator(mCivPositionInterpolator);
        topAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int current = (int) valueAnimator.getAnimatedValue();
                // ViewCompat.setTranslationY(endView, 0);
            }
        });

        ValueAnimator leftAnim = ValueAnimator.ofInt(startLeft, endLeft);
        leftAnim.setDuration(durationCivPosition);
        leftAnim.setInterpolator(mCivPositionInterpolator);
        leftAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int current = (int) valueAnimator.getAnimatedValue();
                //ViewCompat.setTranslationX(endView, 0);
            }
        });

        ValueAnimator sizeAnimator = ValueAnimator.ofInt(startSize, endSize);
        sizeAnimator.setDuration(durationCivSize);
        sizeAnimator.setInterpolator(mCivSizeInterpolator);
        sizeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int current = (int) valueAnimator.getAnimatedValue();
                // endView.getLayoutParams().height = current;
                //  endView.getLayoutParams().width = current;
                //  endView.requestLayout();

                //int curTop = endView.getTop();
                // endView.setBottom(curTop + current);
                //int curLeft = endView.getLeft();
                //endView.setRight(curLeft + current);

            }
        });

        AnimatorSet animSet = new AnimatorSet();
        animSet.play(leftAnim).with(topAnim).with(sizeAnimator);
        //animSet.play(sizeAnimator);
        return animSet;
    }

    /**
     * @param sceneRoot
     * @param startValues
     * @param endValues
     * @return
     */
    private Animator createItemAnim(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
        final View endView = endValues.view;

        final int startTop = (int) startValues.values.get(KEY_ITEM_TOP);
        final int startHeight = (int) startValues.values.get(KEY_ITEM_HEIGHT);

        final int endTop = (int) endValues.values.get(KEY_ITEM_TOP);
        final int endHeight = (int) endValues.values.get(KEY_ITEM_HEIGHT);


        /**
         * 初始化 item endView 的初始化状态-----------------------------
         * ①top和高度是相关联的
         * ②先把top、高度等设置好，再统一刷新UI防止闪动、抖动
         */
        // endView.getLayoutParams().height = startHeight;//不刷新ui
        // ViewCompat.setTranslationY(endView, startTop);//刷新ui
        // endView.requestLayout();//刷新ui
        endView.setTop(startTop);
        endView.setBottom(startTop + startHeight);
        /**
         * 初始化 item endView 的初始化状态-----------------------------
         */


        /**
         * 创建动画
         */
        ValueAnimator topAnim = ValueAnimator.ofInt(startTop, endTop);
        topAnim.setDuration(durationItem);
        topAnim.setInterpolator(mItemInterpolator);
        topAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int current = (int) valueAnimator.getAnimatedValue();
                // ViewCompat.setTranslationY(endView, current);
                endView.setTop(current);
                endView.setBottom(current + startHeight);

            }
        });

        ValueAnimator heightAnimator = ValueAnimator.ofInt(startHeight, endHeight);
        heightAnimator.setDuration(durationItem);
        heightAnimator.setInterpolator(mItemInterpolator);
        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int current = (int) valueAnimator.getAnimatedValue();
                endView.getLayoutParams().height = current;
                endView.requestLayout();
            }
        });

        AnimatorSet animSet = new AnimatorSet();
        animSet.play(heightAnimator).after(topAnim);
        return animSet;
    }

    public void setmItemInterpolator(TimeInterpolator mItemInterpolator) {
        this.mItemInterpolator = mItemInterpolator;
    }

    public void setDurationItem(long durationItem) {
        this.durationItem = durationItem;
    }

}
